package com.jcl.recruit.wechat.logic;

import com.jcl.core.constants.AttachmentConstant;
import com.jcl.core.constants.MimeTypeConstants;
import com.jcl.core.ftp.FtpProxy;
import com.jcl.core.utils.CodeUtils;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.attach.logic.UploadFileLogic;
import com.jcl.recruit.attach.model.UploadFileInfo;
import com.jcl.recruit.attach.vo.AttachmentPhotoVo;
import com.jcl.recruit.core.common.FileFtpProxyFactory;
import com.jcl.recruit.user.logic.UserLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName WechatCommonLogic

 * @Author yyliu
 * @Date 2018/10/24 13:59
 * @Version 1.0
 **/
@Component
public class WechatCommonLogic {

    @Autowired
    private UploadFileLogic uploadFileLogic;

    @Autowired
    private UserLogic userLogic;

    public String updateUserHead(AttachmentPhotoVo photoVo, Integer userId, String corpCode) {

        FtpProxy ftpProxy = null;
        UploadFileInfo uploadFileInfo = null;
        List<UploadFileInfo> fileList = uploadFileLogic.findList(corpCode, userId, AttachmentConstant.FILE_TYPE_PHOTO);
        if (CollectionUtils.isNotEmpty(fileList)) {
            uploadFileInfo = fileList.get(0);
        } else {
            String uuid = CodeUtils.getUuidKey();
            uploadFileInfo = initPhotoFileInfo(photoVo.getFileName(), corpCode, userId, uuid);
        }

        try {
            if (null != photoVo.getBytes()) {
                ftpProxy = FileFtpProxyFactory.getFtpProxy();

                String filePath = FileFtpProxyFactory.getRealFilePath(uploadFileInfo.getCorpCode(),
                        uploadFileInfo.getCategory());
                String newName = FileFtpProxyFactory.getRealFilePathByFilePath(filePath, uploadFileInfo.getExternalName());

                if (CollectionUtils.isNotEmpty(fileList)) {
                    ftpProxy.deleteFile(newName);
                }
                boolean isTrue = ftpProxy.storeFile(photoVo.getBytes(), newName);
                if (isTrue) {
                    if (CollectionUtils.isNotEmpty(fileList)) {

                    } else {
                        uploadFileInfo.setFilePath(filePath);
                        uploadFileLogic.add(uploadFileInfo);
                        userLogic.updateUserHead(userId, uploadFileInfo.getId());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != ftpProxy) {
                ftpProxy.closeServer();
            }
        }

        return uploadFileInfo.getId();

    }

    private UploadFileInfo initPhotoFileInfo(String fileName, String corpCode, Integer userId, String uuid) {
        UploadFileInfo uploadFileInfo = new UploadFileInfo();
        uploadFileInfo.setExternalName(fileName + MimeTypeConstants.PNG_FILE_SUFFIX);
        uploadFileInfo.setCreateTime(new Date());
        uploadFileInfo.setFileType(AttachmentConstant.FILE_TYPE_PHOTO);
        uploadFileInfo.setFileName(fileName);
        uploadFileInfo.getFilePath();
        uploadFileInfo.setCategory(AttachmentConstant.CATEGORY_PHOTO);
        uploadFileInfo.setCorpCode(corpCode);
        uploadFileInfo.setOwnerId(userId);
        uploadFileInfo.setId(uuid);
        uploadFileInfo.setCreateTime(new Date());
        uploadFileInfo.setModifyTime(new Date());
        return uploadFileInfo;
    }
}
